package com.scaffold.photo.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.photo.entity.Photo;
import com.scaffold.springmvc.dao.BasicBizDAO;

@Repository
public class PhotoDAO extends BasicBizDAO<Photo, Long> {

	public List<Photo> findByAlbumId(Long albumId) {
		return this.findListByProperty("albumId", albumId);
	}

	public List<Photo> pageQuery(Page page, Long albumId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (albumId != null) {
			criteria.add(Restrictions.eq("albumId", albumId));
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<Photo> pageQuery(Page page, Long albumId, String title, String path, String remark, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (albumId != null) {
			criteria.add(Restrictions.eq("albumId", albumId));
		}
		if (title != null) {
			criteria.add(Restrictions.eq("title", title));
		}
		if (path != null) {
			criteria.add(Restrictions.eq("path", path));
		}
		if (remark != null) {
			criteria.add(Restrictions.eq("remark", remark));
		}
		if (keyword != null && !keyword.isEmpty()) {
			LogicalExpression expr = Restrictions.or(
					Restrictions.like("title", keyword, MatchMode.ANYWHERE).ignoreCase(),
					Restrictions.like("path", keyword, MatchMode.ANYWHERE).ignoreCase());
			expr = Restrictions.or(expr,Restrictions.like("remark", keyword, MatchMode.ANYWHERE).ignoreCase());
			criteria.add(expr);
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

}
